﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace CheckTicket.Sample
{
    public static class ListBoxBehaviour
    {
        #region AllowCopy

        /// <summary>
        /// 依赖属性：是否允许复制
        /// </summary>
        public static readonly DependencyProperty AllowCopyProperty =
            DependencyProperty.RegisterAttached(cCmdName, typeof(bool), typeof(ListBoxBehaviour)
                , new UIPropertyMetadata(false, _OnAllowCopyChanged));

        /// <summary>
        /// Get:<inheritdoc cref="AllowCopyProperty"/>
        /// </summary>
        /// <param name="d">依赖属性</param>
        /// <returns></returns>
        public static bool GetAllowCopy(DependencyObject d)
        {
            return (bool)d.GetValue(AllowCopyProperty);
        }

        /// <summary>
        /// Set:<inheritdoc cref="AllowCopyProperty"/>
        /// </summary>
        /// <param name="d">依赖属性</param>
        /// <param name="value">属性值</param>
        public static void SetAllowCopy(DependencyObject d, bool value)
        {
            d.SetValue(AllowCopyProperty, value);
        }

        private const string cCmdName = "AllowCopy";

        /// <summary>
        /// 当[<inheritdoc cref="AllowCopyProperty"/>]发生改变时的处理
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void _OnAllowCopyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is not ListBox listBox) { return; }

            switch ((bool)e.NewValue)
            {
                case true:
                    {
                        void CmdHandler(object sender, ExecutedRoutedEventArgs _e)
                        {
                            if (listBox.SelectedItems.Count > 0)
                            {
                                StringBuilder stringBuilder = new StringBuilder();

                                foreach (var lbMsgSelectedItem in listBox.SelectedItems)
                                {
                                    stringBuilder.AppendLine(lbMsgSelectedItem.ToString());
                                }
                                Clipboard.SetDataObject(stringBuilder.ToString());
                            }
                        }

                        var command = new RoutedCommand(cCmdName, typeof(ListBox));
                        command.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Control, "Copy"));
                        listBox.CommandBindings.Add(new CommandBinding(command, CmdHandler));
                    }
                    break;

                case false:
                    {
                        for (int i = 0; i < listBox.CommandBindings.Count; i++)
                        {
                            var cb = listBox.CommandBindings[i];
                            if (cb.Command is RoutedCommand routedCmd)
                            {
                                if (routedCmd.Name == cCmdName)
                                {
                                    listBox.CommandBindings.RemoveAt(i);
                                    break;
                                }
                            }
                        }
                    }
                    break;
            }
        }

        #endregion AllowCopy
    }
}